package com.rainy.cloud.producer.config;

import com.rainy.cloud.jdbc.mybatis.configuration.MybatisConfiguration;
import com.rainy.cloud.jdbc.mybatis.properties.JdbcProperties;
import com.rainy.cloud.jdbc.mybatis.properties.MybatisProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;

@Import(MybatisConfiguration.class)
@Configuration
@ConditionalOnClass(JdbcProperties.class)
public class ProducerMybatisConfiguration {

    @Bean
    public JdbcProperties jdbcProperties(Environment environment) {
        JdbcProperties jdbcProperties = new JdbcProperties();
        jdbcProperties.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
        jdbcProperties.setJdbcUrl(environment.getProperty("spring.datasource.url"));
        jdbcProperties.setUsername(environment.getProperty("spring.datasource.username"));
        jdbcProperties.setPassword(environment.getProperty("spring.datasource.password"));
        return jdbcProperties;
    }

    @Bean
    public MybatisProperties mybatisProperties(Environment environment) {
        MybatisProperties mybatisProperties = new MybatisProperties();
        mybatisProperties.setInterfaceScan(environment.getProperty("mybatis.interfaceScan"));
        mybatisProperties.setXmlScan(environment.getProperty("mybatis.xmlScan"));
        mybatisProperties.setModelScan(environment.getProperty("mybatis.modelScan"));
        mybatisProperties.setTransactionExpress(environment.getProperty("mybatis.transactionExpress"));
        return mybatisProperties;
    }

}
